// deviceApi.ts
import { http, httpTauri } from "@/utils/http";
import { localServer } from "@/api/utils";
import { Result } from "@/api/common";

export interface DeviceProperty {
  model: string;
  property: string;
  displayName: string;
  dataType: "string" | "number" | "enum" | "boolean";
  max: number | null;
  min: number | null;
  precision: number | null;
  unit: string | null;
  defaultValue: string | number | boolean | null;
  enumValues: string[] | null;
  required: boolean;
}

export interface DeviceProperties {
  model: DeviceProperty[];
}

export interface Device {
  id: string;
  model: string;
  spec: string | null;
  name: string;
  memo: string;
  version: string | null;
  parentId: string | null;
  address: string;
  port: string;
  property: Record<string, unknown>;
  createTime: string;
  isSetup: boolean;
  channels: Channel[];
  status: DeviceStatus;
  disabled: boolean;
}

export interface DeviceType {
  value: string;
  label: string;
  icon?: string;
  description?: string;
}

export interface DeviceTypeCategory {
  value: string;
  label: string;
  children: DeviceType[];
}

export const getDeviceProperties = () => {
  return http.request<Result<DeviceProperties>>("get", "/deviceProperties");
};

export const addDeviceProperties = (data?: object) => {
  return http.request<Result<DeviceProperties>>("post", "/deviceProperties", {
    data
  });
};
export const updateDeviceProperties = (data?: object) => {
  return http.request<Result<DeviceProperties>>("put", "/deviceProperties", {
    data
  });
};

export const deleteDeviceProperties = (data?: object) => {
  return http.request<Result<DeviceProperties>>("delete", "/deviceProperties", {
    data
  });
};

export const listDevices = async () => {
  return httpTauri.request<Result<Array<Device>>>(
    "get",
    localServer("/api/lab/device/list")
  );
};

export const listDevicesByMeasure = async (
  measureKind?: string,
  showAll?: string
) => {
  return httpTauri.request<Result<Array<Device>>>(
    "get",
    localServer(
      `/api/lab/device/list?measureKind=${measureKind}&showAll=${showAll}`
    )
  );
};

export const addDevice = (data?: object) => {
  return httpTauri.request<Result<string>>(
    "post",
    localServer("/api/lab/device/Add"),
    {
      data
    }
  );
};

export const deleteDevice = (data?: object) => {
  return httpTauri.request<Result<string>>(
    "post",
    localServer("/api/lab/device/delete"),
    {
      data
    }
  );
};

export const updateDevice = (data?: object) => {
  return httpTauri.request<Result<string>>(
    "post",
    localServer("/api/lab/device/update"),
    {
      data
    }
  );
};

/** 设备类型配置 */
export const getDeviceTypes = () => {
  return http.request<Result<Array<DeviceTypeCategory>>>("get", "/deviceTypes");
};

export interface Channel {
  deviceId: string;
  address: number;
  channelIdx: number;
  enabled: boolean;
  rangeCode: string;
  rangeDesc: string;
  currentValue: number;
  measure: string;
  rangeFrom: number;
  rangeTo: number;
}

export interface DeviceChannel {
  id: string;
  model: string;
  name: string;
  memo: string;
  channels: Channel[];
}

export const findChannel = (model: string, spec: string) => {
  return httpTauri.request<Result<DeviceChannel[]>>(
    "get",
    localServer(`/api/lab/device/findChannel?model=${model}&spec=${spec}`)
  );
};

export interface DeviceStatus {
  name: string;
  model: string;
  deviceId: string;
  onlineState: number;
  scanAmount: number;
  msg: string;
  lastQueryTime: string;
}

export const listDeviceStatus = () => {
  return httpTauri.request<Result<DeviceStatus[]>>(
    "get",
    localServer(`/api/lab/netstate/query`)
  );
};

export const changeOpening = (data?: object) => {
  return httpTauri.request<Result<string>>(
    "post",
    localServer(`/api/Lab/device/valve/changeOpening`),
    { data }
  );
};

export const getConfigDC1000 = (deviceId: string) => {
  return httpTauri.request<Result<string>>(
    "get",
    localServer(`/api/Lab/device/dc1000/GetConfig?deviceId=${deviceId}`)
  );
};

export const ChangeSvDC1000 = (data?: object) => {
  return httpTauri.request<Result<string>>(
    "post",
    localServer(`/api/Lab/device/dc1000/ChangeSv`),
    { data }
  );
};
